require 'andand'
require 'util'
require 'table'

class CashFlowStatement < ActiveRecord::Base
  @@cn2en = {
    '报告期'=>'report_date',
    
    #一、经营活动产生的现金流量    
    '销售商品、提供劳务收到的现金'=>'selling_cash',
    '收取的租金'=>'rent_cash',
    '收到的增值税销项税额和退回的税款'=>'value_added_and_refunded_tax',
    '收到的除增值税以外的其他税费返还'=>'other_returned_tax',
    '收到的其他与经营活动有关的现金'=>'operating_activities_related_cash',
    '经营活动现金流入小计'=>'operating_activities_related_cash_inflow_subtotal',
    '购买商品接受劳务支付的现金'=>'purchase_goods_or_services_paid',
    '经营租赁所支付的现金'=>'operating_lease_paid',
    '支付给职工以及为职工支付的现金'=>'employees_paid',
    '支付的增值税款'=>'vat_paid_tax',
    '支付的所得税款'=>'income_tax',
    '支付的除增值税所得税以外的税费'=>'other_tax',
    '支付的各项税费'=>'tax_subtotal',
    '支付的其他与经营活动有关的现金'=>'other_operating_activities_related_paid',
    '经营活动现金流出小计'=>'operating_activities_cash_outflow_subtotal',
    '经营活动产生的现金流量净额'=>'operating_activities_generated_net_cash_flow',
    
    #二、投资活动产生的现金流量
    '收回投资所收到的现金'=>'recovery_of_investment_cash',
    '取得投资收益所收到的现金'=>'investment_income_cash',
    '分得股利或利润所收到的现金' => 'dividends_or_profits_cash',
    '取得债券利息收入所收到的现金'=>'bond_interest_income_cash',
    '处置固定无形和长期资产收回的现金'=>'long_term_assets_recovery_cash',
    '收到的其他与投资活动有关的现金'=>'other_investment_activities_related_cash',
    '投资活动现金流入小计'=>'investing_activities_cash_inflow_subtotal',
    '购建固定无形和长期资产支付的现金'=>'building_long_term_assets_paid',
    '权益性投资所支付的现金'=>'equity_investment_paid',
    '债权性投资所支付的现金'=>'investment_claims_paid',
    '投资所支付的现金'=>'investment_paid',
    '支付的其他与投资活动有关的现金'=>'other_investment_related_activities_paid',
    '投资活动现金流出小计'=>'investing_activities_cash_outflow_subtotal',
    '投资活动产生的现金流量净额'=>'investment_activities_generated_net_cash_flow',
    
    #三、筹资活动产生的现金流量
    '吸收权益性投资所收到的现金'=>'absorption_of_investment_received_cash',
    '发行债券所收到的现金'=>'issuing_bonds_received_cash',
    '借款所收到的现金'=>'borrowers_received_cash',
    '收到的其他与筹资活动有关的现金'=>'other_financing_activities_related_received_cash',
    '筹资活动现金流入小计'=>'financing_cash_inflow_subtotal',
    '偿还债务所支付的现金'=>'debt_paid',
    '发生筹资费用所支付的现金'=>'financing_costs_paid',
    '分配股利或利润所支付的现金'=>'dividends_or_profits_distribution_paid',
		'偿付利息所支付的现金'=>'interest_paid',
		'融资租赁所支付的现金'=>'finance_leases_paid',
		'减少注册资本所支付的现金'=>'registered_capital_reducing_paid',
		'支付的其他与筹资活动有关的现金'=>'other_financing_activities_related_paid',
		'筹资活动现金流出小计'=>'financing_activities_cash_outflow_subtotal',
		'筹资活动产生的现金流量净额'=>'financing_activities_generated_net_cash_flow',
		
		#四、汇率变动对现金的影响
		'汇率变动对现金的影响' => 'exchange_rate_impact_cash',
		
		#五、现金及现金等价物净增加额
		'现金及现金等价物净增加额' => 'cash_and_cash_equivalents_net_increase',
		
		#附注
		#1、不涉及现金收支的投资和筹资活动
		'以固定资产偿还债务'=>'pay_debt_using_fixed_assets',
		'以固定资产进行长期投资'=>'long_term_investment_using_fixed_assets',
		'以投资偿还债务'=>'pay_debt_using_invest',
		'融资租赁固定资产'=>'finance_leases_of_fixed_assets',
		'以存货偿还债务'=>'pay_debt_using_inventory',
 
		#2、将净利润调节为经营活动的现金流量
		'净利润'=>'net_income',
		'少数股东损益'=>'minority_gains_and_losses',
		'计提的资产减值准备'=>'assets_impairment_provision',
		'计提的坏帐准备或转销的坏帐'=>'bad_debt_provision',
		'固定资产折旧'=>'fixed_assets_depreciation',
		'无形资产摊销'=>'intangible_assets_amortization',
		'递延资产摊销'=>'deferred_assets_amortization',
		'长期待摊费用摊销'=>'long_term_prepaid_expenses_amortization',
		'待摊费用的减少(减增加)'=>'deferred_expenses_reduction',
		'预提费用的增加(减减少)'=>'accrued_expenses_increase',
		'处置固定无形和其他长期资产的损失(减：收益)'=>'intangible_fixed_and_long_term_assets_disposal_losses',
		'固定资产报废损失'=>'fixed_assets_end_of_life_losses',
		'财务费用'=>'finance_charges',
		'投资损失(减收益)'=>'investment_losses',
		'递延税款贷项(减借项)'=>'deferred_tax_credit',
		'存货的减少(减增加)'=>'inventory_reduction',
		'经营性应收项目的减少(减增加)'=>'operating_receivables_reduction',
		'经营性应付项目的增加(减减少)'=>'operating_payables_increase',
		'增值税增加净额(减减少)'=>'value_added_tax_net_increase',
		'其他'=>'other_cash_flow',
			
		#3、现金及现金等价物净增加情况
		'货币资金的期末余额'=>'funds_end_balance',
		'货币资金的期初余额'=>'funds_opening_balance',
		'现金等价物的期末余额'=>'cash_equivalents_end_balance',
		'现金等价物的期初余额'=>'cash_equivalents_opening_balance',
		'现金及现金等价物净增加额'=>'cash_and_cash_equivalents_net_increase',
		
		#特殊项目		
		'子公司吸收少数股东权益性投资所收到的现金'=>'subsidiary_minority_shareholders_investment_cash',
		'子公司支付给少数股东的利润'=>'subsidiary_minority_shareholders_profits_paid',
		'未确认投资损失'=>'unrecognized_investment_losses',
		'计提的存货跌价损失准备'=>'inventory_loss_provision',
		'计提的固定资产减值准备'=>'fixed_assets_impairment_provision',
		'计提的无形资产减值准备'=>'intangible_assets_impairment_provision',
		'利息及投资收入'=>'interest_and_investment_income',
		'合并借差(减:贷差)摊销'=>'borrow_difference_amortization',
		'购买日前净利润'=>'day_before_buy_net_profit',
		'其他长期负债摊销'=>'other_long_term_debt_amortization',
		'冲转固定资产评估增值部分的折旧'=>'fixed_assets_value_added_part_depreciation',
		'开办费摊销'=>'start_up_costs_amortization'    
  }
  
  @@en2cn = {}

	def self.cn2en
		@@cn2en
	end

  def self.en2cn
    if @@en2cn.empty?
      @@cn2en.each { |key, value| @@en2cn[value] = key }
    end
    @@en2cn
  end

  def self.find_by_stock_no_from_sina(stock_no)
  	return Util.find_data_by_from_sina(
			"http://money.finance.sina.com.cn/corp/go.php/vFD_CashFlow/stockid/#{stock_no}/ctrl/part.phtml",
			'ProfitStatementNewTable',
			@@cn2en
		)
		
  end
end

